// import assert from 'nanoassert'
import { decoders, tag } from "./wire-types.js";

export default function* reader(
	buf,
	byteOffset = 0,
	byteLength = buf.byteLength,
) {
	let o = byteOffset;
	const end = byteOffset + byteLength;
	// assert(end <= buf.byteLength)

	for (; o < end; ) {
		const [{ wireType, fieldNumber }, tagLen] = tag(buf, o),
			tagOffset = o;
		o += tagLen;

		const [data, len] = decoders[wireType](buf, o);
		o += len;

		yield [
			fieldNumber,
			{
				tagOffset,
				tagLen,
				fieldNumber,
				wireType,
				len,
				data,
			},
		];
	}

	// assert(o === end)
}
